from django.db import models

# Create your models here.

#商品种类模型
class TypeInfo(models.Model):
    ttitle = models.CharField(max_length=20)   #分类名称
    isDelete = models.BooleanField(default=False)  #逻辑删除

    def __str__(self):
        return self.ttitle

#商品模型
class GoodsInfo(models.Model):
    gtitle = models.CharField(max_length=50)                    #商品名称
    gpic = models.ImageField(upload_to='goods')                 #图片
    gprice = models.DecimalField(max_digits=5,decimal_places=2) #单价
    isDelete = models.BooleanField(default=False)               #逻辑删除
    gunit = models.CharField(max_length=20,default='500g')      #单位
    gclick = models.IntegerField()                              #点击量，人气
    gjianjie = models.CharField(max_length=200)                 #简介
    gkucun = models.IntegerField()                              #库存
    gcontent = models.TextField()                               #描述
    gtype = models.ForeignKey(TypeInfo)                         #外键关联类型

    def __str__(self):
        return self.gtitle

#用户信息模型
class Usermessage(models.Model):
    user_name = models.CharField(max_length=20)  #用户名
    password = models.CharField(max_length=64)   #密码
    email = models.EmailField(max_length=255)    #email
    phone = models.CharField(max_length=11)      #联系电话
    home = models.CharField(max_length=50,default=0)  #地址

    def __str__(self):
        return self.user_name

#收货地址模型
class Goodsarea(models.Model):
    aname = models.CharField(max_length=20)  #收件人
    ahome = models.CharField(max_length=100)     #收件地址
    aemailnum = models.CharField(max_length=6)   #邮编
    aphone = models.CharField(max_length=11)    #手机
    isDelete = models.BooleanField(default=False)  # 逻辑删除
    auser = models.ForeignKey('Usermessage')  #用户关联多个收货地址

    def __str__(self):
        return self.aname

#-------------------------------------------------------------------
#购物车模型
class CarInfo(models.Model):
    user = models.ForeignKey('Usermessage')
    goods = models.ForeignKey('GoodsInfo')
    count = models.IntegerField()       #数量

#订单模型-主表
class OrderInfo(models.Model):
    oid = models.CharField(max_length=20,primary_key=True) #订单编号：由当前时间+用户编号构成
    user = models.ForeignKey('Usermessage')     #订单用户
    odate = models.DateTimeField(auto_now_add=True)   #下单日期
    oispay = models.BooleanField(default=False)       #是否支付
    ototal = models.DecimalField(max_digits=6,decimal_places=2)   #金额总计
    oaddress = models.CharField(max_length=150)       #收货地址

#订单模型-从表
class OrderDetailInfo(models.Model):
    goods = models.ForeignKey('GoodsInfo')   #商品
    order = models.ForeignKey('OrderInfo')   #订单
    price = models.DecimalField(max_digits=5,decimal_places=2)  #价格
    count = models.IntegerField()           #数量

